$(document).ready(function(){	
	var layer_time = 1500;
	// 刷新
	$(".refresh").click(function(){
		location.reload();

	});

	//后退
	$(".goback").click(function(){
		window.history.back();
	});

	// 普通提交
	$("#ajaxform").ajaxForm({
		success:complete,
		dataType:"json"
	});
	
	function complete(data){
		layer.msg(data.msg,{time:layer_time});
	}

	// 提交表单后刷新页面
	$("#ajaxform_reload").ajaxForm({
		beforeSubmit:shadow_page,
		success:complete_reload,
		dataType:"json"
	});

	function complete_reload(data){
		parent.layer.close(parent.layer.index);
		if(data.code == 1){
			layer.msg(data.msg,{time:layer_time},function(){
				location.reload();
			});
		}else{
			layer.msg(data.msg,{time:layer_time});
		}
	}

	// 提交表单后跳转页面
	$("#ajaxform_gourl").ajaxForm({
		beforeSubmit:shadow_page,
		success:complete_gourl,
		dataType:"json"
	});

	function complete_gourl(data){
		parent.layer.close(parent.layer.index);
		layer.msg(data.msg,{time:layer_time},function(){
			if(data.code){
				window.location.href = data.url;
			}else{
				return false;
			}
		});
	}

	// 提交之后关闭 layer open页面
	$("#ajaxform_close").ajaxForm({
		beforeSubmit:shadow_page,
		success:complete_close,
		dataType:"json"
	});

	function complete_close(data){
		parent.layer.close(parent.layer.index);
		layer.msg(data.msg,{time:layer_time},function(){
			if(data.code){
				parent.location.reload();
			}else{
				return false;
			}
		});
	}

	


	
	
    // 选择省市区 JS
    $(".province").change(function(){
    	var province_id = $(this).val();
    	if(province_id == ''){
    		return false;
    	}
    	$.post('/Home/Region/provinceChange',{'province_id':province_id},function(data){
    		var city = data.city;
    		var town = data.town;
    		var	city_select = null;
    		var town_select = null;
    		$(city).each(function(k,v){
    			city_select+="<option value='"+v.id+"'>"+v.name+"</option>";
    			$(".city").html(city_select);
    		});
    		$(town).each(function(k,v){
    			town_select+="<option value='"+v.id+"'>"+v.name+"</option>";
    			$(".town").html(town_select);
    		});
    	});
    });

    $(".city").change(function(){
    	var city_id = $(this).val();
    	if(city_id == ''){
    		return false;
    	}
    	$.post('/Home/Region/cityChange',{'city_id':city_id},function(data){
    		var town = data.town;
    		var town_select = null;
    		$(town).each(function(k,v){
    			town_select+="<option value='"+v.id+"'>"+v.name+"</option>";
    			$(".town").html(town_select);
    		});
    	});
    });

    // 上传图片JS
    $("#img_upload").change(function () { 
	    $("#img_box").html('');
	    if ( typeof (FileReader) != "undefined" ) { 
	      var regex = /(.jpg|.jpeg|.gif|.png|.bmp)$/; 
	      $($(this)[0].files).each(function () { 
	        var file = $(this); 
	        if (regex.test(file[0].name.toLowerCase())) { 
	            var reader = new FileReader(); 
	            reader.onload = function (e) { 
	                var haslength = $("#img_box img").length;
	                if(haslength > 8){
	                  return false;
	                }
	                $("#img_box").append("<img  src='"+e.target.result+"' style='max-width:80px;max-height:80px;margin-right:2%;margin_bottom:2%;' />"); 
	            } 
	            reader.readAsDataURL(file[0]); 
	        } else { 
	            return false; 
	        } 
	      }); 

	    } else { 
	      alert("暂不支持H5上传图片"); 
	    } 
    });



    function shadow_page(){
		var close_index = parent.layer.load(1, {
		  shade: [0.01,'#000'], //0.1透明度的白色背景
		});
	}
});